<?php

declare(strict_types=1);

namespace app\controller\admin;

use app\model\admin\ApplyMoneySet;
use app\model\admin\ApplyPay;
use app\model\admin\CateModel;
use app\model\admin\Pact;
use think\facade\Validate;
use think\Request;

class ApplyPayController extends AdminBaseController
{
    public function change(Request $request)
    {
        $id = input('id');
        $params = $request->param();
        $member_user = $request->authInfo;
        $user_id = $member_user['user_id'];
        if (!isset($params['merchant_key']) || !isset($params['merchant_id'])) {
            return error('请将数据填写完整');
        }
        if (!isset($params['cert_file']) || !isset($params['key_file'])) {
            return error('请将数据填写完整');
        }
        if (strlen($params['merchant_key']) != 32) {
            return error('密钥必须是32位长度');
        }
        if ($id) {
            // 修改
            $info = ApplyPay::find($id);

            if (!strstr($params['merchant_key'], "**")) {
                $info->merchant_key = $params['merchant_key'];
            }
            $info->merchant_id = $params['merchant_id'];
            $info->cert_file = $params['cert_file'];
            $info->key_file = $params['key_file'];
            $info->save();
        } else {
            // 新增
            // $params['sys_user_id'] = $user_id;
            $res = ApplyPay::create($params);
        }
        return success();
    }
    public function getInfo(Request $request)
    {
        $member_user = $request->authInfo;
        $user_id = $member_user['user_id'];

        $res = ApplyPay::find(1);
        // unset($res->merchant_key);
        if ($res) {
            // $res->merchant_key = "********";
        } else {
            $res = [];
        }

        $res['tip'] = "<div>提示：需要微信支付商户开通<span style='color: #f00'>【企业打款到零钱】</span>产品功能，具体详情请登录微信支付官方后台=》产品中心查看</div>";

        return success($res);
    }
}
